package com.enzoism.spring.controller;

import com.enzoism.spring.model.User;
import com.enzoism.spring.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;

/**
 * Created with IntelliJ IDEA.
 * User: enzoism
 * Date: 2020/2/6- 23:35
 */
@Controller
@Slf4j
public class UserController {

    @Resource
    UserService userService;

    // 登录页面跳转
    @RequestMapping(value = {"/","/index","/login"},method = RequestMethod.GET)
    public String loginPage(){
        System.out.println("------------login");
        return "index";
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(@RequestParam String email, @RequestParam String password){
        User loginUser = userService.getUserByEmailAndPassword(email, password);
        if (null != loginUser){
            log.info("------------>当前用户登录成功user："+loginUser);
            return "redirect:/main/index";
        }else{
            log.warn("------------>当前用户登录失败email："+email+"  重新跳转登录页面");
            return "index";
        }
    }
}
